

DELIMITER //
DROP TRIGGER set_active_on_items_insert //
CREATE TRIGGER set_active_on_items_insert BEFORE INSERT ON sklad.items
 FOR EACH ROW
 BEGIN

    IF NEW.active <> 'D' THEN
        IF NEW.sell_price <> 0 && (NEW.stock_quantity - NEW.reserv_quantity > 0 || NEW.stock = 'Y') THEN
            SET NEW.active = 'Y';
        ELSE
            SET NEW.active = 'N';
        END IF;
    END IF;

 END //
DELIMITER ;
